package com.hmall.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.constants.MqConstants;
import com.hmall.search.mapper.ItemMapper;
import com.hmall.search.pojo.BaseContext;
import com.hmall.search.pojo.Item;
import com.hmall.search.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageDTO<Item> pageItem(Integer page, Integer size) {
        Page<Item> itemPage = new Page<>(page,size);
        itemMapper.selectPage(itemPage,null);
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setList(itemPage.getRecords());
        itemPageDTO.setTotal(itemPage.getTotal());
        return itemPageDTO;
    }

    @Override
    public Item getItemById(Long id) {
        if (id == null){
            log.error("id为空！");
            return null;
        }
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,id);
        Item item = itemMapper.selectOne(qw);
        if (item == null){
            log.error("该商品不存在!");
            return null;
        }
        return item;
    }

    @Override
    public void addItem(Item item) {
        if (item == null){
            log.error("数据为空");
            return;
        }
        itemMapper.insert(item);
    }

    @Override
    public void updateItemStatus(Long id, Integer status) {
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,id);
        Item item1 = itemMapper.selectOne(qw);
        if (item1 == null){
            log.error("商品不存在");
            return;
        }
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);
        if (status == 1){
            //上架
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,id);
        }else if (status == 2){
            //下架
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);
        }
    }

    @Override
    public void updateItemById(Item item) {
        Long id = item.getId();

        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,id);
        Item item1 = itemMapper.selectOne(qw);
        if (item1 == null){
            log.error("商品不存在");
            return;
        }
        itemMapper.updateById(item);
    }

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,id);
        Item item1 = itemMapper.selectOne(qw);
        if (item1 == null){
            log.error("商品不存在");
            return;
        }
        itemMapper.deleteById(id);
    }

    @Override
    public void deleteStock(Long id, Integer num) {
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,id);
        Item item = itemMapper.selectOne(qw);
        //判断库存
        Integer stock = item.getStock();
        if (stock == 0 ){
            log.error("库存为空");
            throw new RuntimeException();
        }else if (stock < num){
            log.error("当前库存不满足需求");
            throw new RuntimeException();
        }
        stock = stock - num;
        item.setStock(stock);
        itemMapper.updateById(item);
    }

    @Override
    public void addStock(Long itemId, Integer num) {
        LambdaQueryWrapper<Item> qw = new LambdaQueryWrapper<>();
        qw.eq(Item::getId,itemId);
        Item item = itemMapper.selectOne(qw);
        Integer stock = item.getStock();
        stock += num;
        item.setStock(stock);
        itemMapper.updateById(item);
    }


}
